<?php

/**
 * Default_Model_Mapper_User
 *
 * @category   Beaver
 * @package    Beaver
 * @subpackage Beaver_Model_Mapper_Db
 * @author     Mihály Kuprivecz
 */
class Default_Model_User_Mapper extends Beaver_Model_Mapper_Db
{
	/**
	 * Find a user by username in Db
	 *
	 * @param string $username
	 * @return void
	 */
	public function findByUsername( $username )
	{
		$where      = $this->getDbTable()->getAdapter()->quoteInto('username = ?', $username);
		$result     = $this->getDbTable()->fetchAll( $where );
		$model      = $this->getModel ();
		$attributes = $model->getAttributes();
		foreach ( $result as $row )
		{
			foreach ($attributes as $name => $value)
			{
				if ( isset($row->$name) ) $model->$name = $row->$name;
			}
		}
	}
	
	/**
	 * Set the last login time in db
	 *
	 * @return void
	 */
	public function setLastLogin()
	{
		$model    = $this->getModel();
		$username = $model->username; 
		$where    = $this->getDbTable()->getAdapter()->quoteInto('username = ?', $username);
		$this->getDbTable()->update( array("lastlogin" => date( "Y-m-d H:i:s", time() ) ), $where );
	}
	
}
